package com.shangcheng.background.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.shangcheng.goods.feign.BrandFeign;
import com.shangcheng.goods.feign.SpuFeign;
import com.shangcheng.goods.pojo.Goods;
import com.shangcheng.goods.pojo.Sku;
import com.shangcheng.goods.pojo.Spu;
import com.shangcheng.users.feign.UserFeign;
import com.shangcheng.users.pojo.User;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/bg")
public class BackgroundController {
    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private BrandFeign brandFeign;


    @GetMapping(value = "/login")
    public String login() {
        return "bml";
    }


    @GetMapping(value = "/addProduct")
    @ResponseBody
    public String addProduct() {
        return "addProduct";
    }

    @GetMapping(value = "/brandManger")
    public String brandManger(@RequestParam int page, ModelMap model) {
        Result<PageInfo> brands = brandFeign.findPageBrand(page, 20);
        model.addAttribute("brands", brands.getData().getList());
        model.addAttribute("page", brands.getData().getPageNum());
        return "brandManger";
    }

    @GetMapping(value = "/productList")
    public String productList(@RequestParam int page, ModelMap model) {
        Result<PageInfo> good = spuFeign.findPageGoods(page, 20);
        model.addAttribute("goods", good.getData().getList());
        model.addAttribute("page", good.getData().getPageNum());
        return "productList";
    }

    @GetMapping(value = "/goods")
    @ResponseBody
    public List goods(@RequestParam int page) {
        Result<PageInfo> good = spuFeign.findPageGoods(page, 20);
        return good.getData().getList();
    }

    @GetMapping(value = "/userList")
    public String userList(@RequestParam int page, ModelMap model) {
        Result<PageInfo> users = userFeign.findPage(page, 20);
        model.addAttribute("users", users.getData().getList());
        model.addAttribute("page", users.getData().getPageNum());
        return "userList";
    }

    @PostMapping(value = "/loginsc")
    public String loginsc(@RequestParam String username,
                          @RequestParam String password, ModelMap model) {
        Result<User> user = userFeign.auth(username, password);
        if (user.getCode() == 20002) {
            model.addAttribute("save", user.getMessage());
            return "bml";
        } else {
            Result<PageInfo> good = spuFeign.findPageGoods(1, 20);
            List list = new ArrayList();
            list.add(user.getData());
            String jsonList = JSON.toJSONString(list);
            List<User> list1 = JSON.parseArray(jsonList, User.class);
            User users = list1.get(0);
            LinkedHashMap hashMap = new LinkedHashMap();
            hashMap.put("username", users);
            if (users.getUsername().equals("123")) {
                model.addAttribute("username", username);
                model.addAttribute("goods", good.getData().getList());
                model.addAttribute("page", good.getData().getPageNum());
                return "bgindex";
            } else {
                model.addAttribute("save", "不是管理员");
                return "bml";
            }
        }
    }

    @GetMapping(value = "/isMarketable")
    public String Marketable(@RequestParam int id,
                             @RequestParam String isMarketable,
                             @RequestParam int page,
                             ModelMap model) {
        Result<PageInfo> good = spuFeign.findPageIsMarketable(isMarketable, page, 20);
        model.addAttribute("goods", good.getData().getList());
        model.addAttribute("page", good.getData().getPageNum());
        return "bgindex";
    }

    @GetMapping(value = "/status")
    public String Status(@RequestParam int id,
                         @RequestParam String status,
                         @RequestParam int page,
                         ModelMap model) {
        Result<PageInfo> good = spuFeign.findPageByStatus(status, page, 20);
        model.addAttribute("goods", good.getData().getList());
        model.addAttribute("page", good.getData().getPageNum());
        return "bgindex";
    }

    @GetMapping(value = "/background-su")
    public String background(@RequestParam Long id,
                             ModelMap model) {
        Result good = spuFeign.findById(id);
        model.addAttribute("goods", good.getData());
        return "background-su";
    }

    @GetMapping(value = "/find")
    public String back(@RequestParam Long id,
                       ModelMap model) {
        Result good = spuFeign.findById(id);
        model.addAttribute("goods", good.getData());
        return "background-su";
    }


    @GetMapping(value = "/dex")
    public String findPageGoods(@RequestParam int page, ModelMap model) {
        Result<PageInfo> good = spuFeign.findPageGoods(page, 20);
        model.addAttribute("goods", good.getData().getList());
        model.addAttribute("page", page);
        return "background";
    }

    @GetMapping("/save")
    public String save(@RequestParam Integer brandId,
                       @RequestParam String name,
                       @RequestParam String sn,
                       @RequestParam String image,
                       @RequestParam Integer price,
                       @RequestParam String spec,
                       ModelMap model) {
        Goods goods = new Goods();
        Spu spu = new Spu(sn, name, brandId, image);
        List<Sku> skuList = new ArrayList<Sku>();
        Sku sku = new Sku();
        sku.setSpuId(spu.getId());
        sku.setPrice(price);
        sku.setSpec(spec);
        skuList.add(sku);
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        Result good = spuFeign.save(goods);
        Result<PageInfo> gods = spuFeign.findPageGoods(1, 20);
        model.addAttribute("goods", gods.getData().getList());
        model.addAttribute("save", good.getMessage());
        return "background";
    }

    @GetMapping("/update")
    public String update(
            @RequestParam Long id,
            @RequestParam Integer brandId,
            @RequestParam String name,
            @RequestParam String sn,
            @RequestParam String image,
            @RequestParam Integer price,
            @RequestParam String spec,
            ModelMap model) {
        Goods goods = new Goods();
        Spu spu = new Spu();
        spu.setId(id);
        spu.setBrandId(brandId);
        spu.setName(name);
        spu.setSn(sn);
        spu.setImage(image);
        List<Sku> skuList = new ArrayList<Sku>();
        Sku sku = new Sku();
        sku.setSpuId(spu.getId());
        sku.setPrice(price);
        sku.setSpec(spec);
        skuList.add(sku);
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        Result good = spuFeign.update(goods);
        model.addAttribute("save", good.getMessage());
        Result gods = spuFeign.findById(id);
        model.addAttribute("goods", gods.getData());
        return "background-su";
    }


    @PostMapping("/logic/delete")
    public String logicDelete(@RequestParam Long id,
                              @RequestParam int page, ModelMap model) {
        Result good = spuFeign.logicDelete(id);
        Result<PageInfo> god = spuFeign.findPageGoods(page, 20);
        model.addAttribute("goods", god.getData().getList());
        model.addAttribute("page", god.getData().getPageNum());
        return "bgindex";
    }


}
